﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public partial class UserDefinedFunctions
{
    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlString OptionPnL()
    {
        settings.Expiry = position.OptionExpireDate;
        var currentPrice = (decimal) GetOptionCurrentPrice(position, tick, settings);
        var cross = tick.CrossPrice;

        decimal currentpnl = 0;

        decimal? pnl = position.IsBuyPostion
                           ? currentPrice*(decimal) position.LotCount - (decimal) position.OpenPrice
                           : (decimal) position.OpenPrice - currentPrice*(decimal) position.LotCount;
        pnl = (cross.HasValue ? pnl*cross : pnl) - position.Fees*(decimal) position.LotCount -
              position.Commissions*(decimal) position.LotCount;
            currentpnl = pnl ?? 0;

        return currentpnl;
    }
};

